﻿using SuperJoe.Framework.Common;
using SuperJoe.Framework.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WWX.MemberPortal.IDAL;
using WWX.Model;

namespace WWX.MemberPortal.BLL
{
    public class MemberBLL : BaseBLL<IMemberDAL>
    {
        public void UpdatePwd(string account, string oldPwd, string newPwd)
        {
            var member = this.Get(account, oldPwd);
            if (member == null)
            {
                ThrowHelper.ThrowFriendlyException("旧密码错误");
            }

            this.dal.UpdatePwd(member.Id, MD5.Encrypt3DES(newPwd));
        }

        public Member Get(string account, string pwd)
        {
            var commonMember = this.GetCommonMember();
            if (commonMember != null && commonMember.Account == account && commonMember.Pwd == pwd)
            {
                return commonMember;
            }

            var encrypedPwd = MD5.Encrypt3DES(pwd);
            return this.dal.Get(account, encrypedPwd);
        }

        public Member Get(string id)
        {
            return this.dal.Get(id);
        }

        public void UpdateAddress(string id, string address)
        {
            this.dal.UpdateAddress(id, address);
        }


        private Member GetCommonMember()
        {
            try
            {
                var customerMember = ConfigUtil.GetValue<string>("CustomerAccountAndPwd").Split(':');
                var account = customerMember[0].Trim();
                var pwd = customerMember[1].Trim();
                return new Member()
                {
                    Id = "0000",
                    Account = account,
                    NickName = "通用账号",
                    TrueName = "通用账号"
                };
            }
            catch (Exception ex)
            {

                return null;
            }

        }
    }
}
